 /* 
 * File:   State.cpp
 * Author: josep
 * 
 * Created on 5 / desembre / 2010, 05:25
 */

#include "State.h"
using namespace std;

#ifndef STATE_CPP
#define	STATE_CPP


template <class T>
State<T>::State(){
}

template <class T>
State<T>::State(int id){
    this->id = id;
}

/*
template <class T>
State<T>::State(const State<T>& orig) {
    this->id=orig.id;
    this->name = orig.name;
    this->owner=orig.owner;
    typename map<int, State<T>* >::const_iterator end = orig.transitionMap.end();
    typename map<int, State<T>* >::const_iterator it = orig.transitionMap.begin();
    while(it!=end){
        this->transitionMap[it->first]=it->second;
        it++;
    }
    for(int mod=0; mod<2; mod++){
        list<int>::iterator end = orig.changeStateListener[mod].end();
        list<int>::iterator it = orig.changeStateListener[mod].begin();
        while(it!=end){
            this->changeStateListener[mod][it]=(*it);
            it++;
        }
    }
    for(int mod=0; mod<2; mod++){
        map<int, list<int> >::const_iterator end = orig.transitionListeners[mod].end();
        map<int, list<int> >::const_iterator it = orig.transitionListeners[mod].begin();
        while(it!=end){
            this->transitionListeners[mod][it->first]=it->second;
            it++;
        }
    }
}
 * */

template <class T>
State<T>::~State() {
}

template <class T>
void State<T>::setTransition(int transitionValue, State<T> *stateToChange){
    transitionMap[transitionValue]= stateToChange;
}

template <class T>
string State<T>::getName(){
    return name;
}

template <class T>
int State<T>::getId(){
    return id;
}

template <class T>
void State<T>::setId(int id){
   this->id=id;
}
template <class T>
void State<T>::setOwner(T* owner) {
    this->owner = owner;
}

template <class T>
T* State<T>::getOwner() const {
    return owner;
}

template <class T>
void State<T>::setName(string name){
    this->name = name;
}

template <class T>
State<T> * State<T>::getStateFromTransitionValue(int transitionValue){
    State<T> *ret=0x0;
    if(this->transitionMap.find(transitionValue)!=transitionMap.end()){
        ret = this->transitionMap[transitionValue];
    }
    return ret;
}

template <class T>
void State<T>::addSubscription(int subscriptionMode, int transitionValue,
              int (T::*cb)(int,int,int)){
    this->transitionListeners[subscriptionMode][transitionValue].push_back(cb);
}

template <class T>
void State<T>::addSubscription(int subscriptionMode,
              int (T::* callBackFunction)(int,int,int)){
    this->changeStateListener[subscriptionMode].push_back(callBackFunction);
}

template <class T>
int State<T>::runSubscribers(int subscriptionMode
                            , int transitionValue, int stateId){
    //State<T>::CallBackFunctionReturn ret=CONTINUE;
    int ret=CONTINUE;
    typename list<int (T::*)(int, int, int)>::iterator it;
    int oldStateId, newStateId;
    if(subscriptionMode==SUBSCRIPTION_OUT){
        oldStateId=id;
        newStateId=stateId;
    }else{
        oldStateId=stateId;
        newStateId=id;
    }
    for(it=transitionListeners[subscriptionMode][transitionValue].begin()
            ; ret==CONTINUE && it!=transitionListeners[subscriptionMode][transitionValue].end()
            ; it++){
        ret=(owner->*(*it))(transitionValue, oldStateId, newStateId);
    }
    if(ret==CONTINUE){
        for(it=changeStateListener[subscriptionMode].begin()
                ; ret==CONTINUE && it!=changeStateListener[subscriptionMode].end()
                ; it++){
            ret=(owner->*(*it))(transitionValue, oldStateId, newStateId);
        }
    }
    return ret;
}


#endif	/* STATE_CPP */
